package com.zdy.im.user.dao;


import java.util.Collection;
import java.util.Map;

import com.sixeco.framework.common.enums.TbStatusEnum;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.entity.ImRoomUserShip;
import com.zdy.im.user.mapper.ImRoomUserShipMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;




@Service("imRoomUserShipDao")
public class ImRoomUserShipDao extends MyBatisBaseCacheDao<ImRoomUserShipMapper, ImRoomUserShip> {

    @Autowired
    private ImRoomUserShipMapper  imRoomUserShipMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImRoomUserShip>  iPage =  new Query<ImRoomUserShip>().getPage(params);

        QueryWrapper<ImRoomUserShip> query =  new QueryWrapper<>();

        IPage<ImRoomUserShip> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImRoomUserShip> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImRoomUserShip.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }

    public List<ImRoomUserShip> selectByUser(long userId) {
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", TbStatusEnum.ENABLE.index());
        List<ImRoomUserShip> resDtoList = imRoomUserShipMapper.selectList(queryWrapper);
        return resDtoList;
    }


    /**
     * 查询某个用户，所有添加的群
     * @param userId
     * @return
     */
    public List<ImRoomUserShip> selectListByUserId(long userId){
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status",1)
                .orderByDesc("create_date");
        return this.list(queryWrapper);
    }


    public ImRoomUserShip selectRoomFriend(long userId,long roomId){
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("room_id",roomId)
                .eq("status",1);
        //return this.getOne(queryWrapper,false);
        List<ImRoomUserShip> imRoomUserShips = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imRoomUserShips)){
            return null;
        }
        return imRoomUserShips.get(0);
    }

    /**
     * 查询某个群，所有好友
     * @param roomId
     * @return
     */
    public List<ImRoomUserShip> selectListByRoomId(long roomId){
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId)
                .eq("status",1)
                .orderByDesc("create_date");
        return this.list(queryWrapper);
    }

    /**
     * 查询某个群，所有好友
     * @param imRoomUserShip
     * @return
     */
    public List<ImRoomUserShip> selectListByRoomId(ImRoomUserShip imRoomUserShip){
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        if(imRoomUserShip.getRoomId() > 0){
            queryWrapper.eq("room_id",imRoomUserShip.getRoomId());
        }

        if(imRoomUserShip.getForbidSpeech() > 0){
            queryWrapper.eq("forbid_speech",imRoomUserShip.getForbidSpeech());
        }else if(imRoomUserShip.getForbidSpeech() == -1){
            queryWrapper.eq("forbid_speech",0);
        }

        if(StringUtils.isNotBlank(imRoomUserShip.getNickName())){
            queryWrapper.like("nick_name",imRoomUserShip.getNickName());
        }

        queryWrapper.eq("status",1)
                .orderByDesc("create_date");
        return this.list(queryWrapper);
    }

    public List<ImRoomUserShip> selectRoomFriend(Collection<Long> userIds, long roomId){
        QueryWrapper<ImRoomUserShip> queryWrapper = new QueryWrapper<>();
        if(!CollectionUtils.isEmpty(userIds)){
            queryWrapper.in("user_id", userIds);
        }
        queryWrapper.eq("room_id",roomId)
                .eq("status",1);
        List<ImRoomUserShip> imRoomUserShips = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imRoomUserShips)){
            return null;
        }
        return imRoomUserShips;
    }
}